﻿<?php

class Admin_Form_UserAdmin extends Zend_Form {

	const EMAIL_LABEL			= 'Email';
	const PASSWORD_LABEL		= 'Password';
	const ROLE_LABEL			= 'Quyền hạn';
	const FULLNAME_LABEL		= 'Họ tên';
	const BIRTHDAY_LABEL		= 'Ngày sinh';
	const TEL_LABEL				= 'Điện thoại';

	const ENCODING				= 'UTF-8';
	const PASSWORD_MIN_LENGTH	= 6;
	const PASSWORD_MAX_LENGTH	= 32;

	protected function addEmail() {
		$tb_name = new Admin_Model_DbTable_UsersAdmin();

		$email = new Zend_Form_Element_Text('email');
		$email->setLabel(self::EMAIL_LABEL)
			  ->setRequired(true)
			  ->addValidator('NotEmpty', true)
			  ->setAttribs(array("maxlength" => "100"))
			  ->addValidator('Db_NoRecordExists', false, array(
			  												'table' => $tb_name->getTbName(),
			  												'field' => 'email',
			  												'messages' =>array(
			  													'recordFound' => self::EMAIL_LABEL . " '%value%' đã tồn tại."
			  												)
			  											)
			  				)
			  ->addFilter('StripTags')
			  ->addFilter('StringTrim')
			  ->addValidator(new Zendhlt_Validate_Email());

		return $email;
	}

	protected function addPassword() {
		$passwordLength = new Zend_Validate_StringLength(self::PASSWORD_MIN_LENGTH, self::PASSWORD_MAX_LENGTH, self::ENCODING);

		$password = new Zend_Form_Element_Password('password');
		$password->setLabel(self::PASSWORD_LABEL)
				 ->setRequired(true)
				 ->addValidator('NotEmpty', true)
				 ->addFilter('StripTags')
				 ->addFilter('StringTrim')
				 ->setAttribs(array("maxlength" => "32"))
				 ->addValidator($passwordLength);

		return $password;
	}

	protected function addRole() {
		$notEmptyRole = new Zend_Validate_NotEmpty();
		$notEmptyRole->setMessage('Chưa chọn quyền hạn!');

		$role = new Zend_Form_Element_Select('role');
		$role->setLabel('Quyền hạn')
			 ->setRequired(true)
			 ->addValidator($notEmptyRole, true)
			 ->setAttrib('key', 'value')
			 ->addFilter('StripTags')
			 ->addFilter('StringTrim')
			 ->setRegisterInArrayValidator(false);

		$role->addMultiOption("", "  Chọn quyền hạn");
		return $role;
	}
	
	protected function addFullName() {
		$fullname = new Zend_Form_Element_Text('ho_ten');
		$fullname->setLabel(self::FULLNAME_LABEL)
				 ->addFilter('StripTags')
				 ->addFilter('StringTrim')
				 ->setRequired(true)
				 ->addValidator('NotEmpty', true)
				 ->setAttribs(array("maxlength" => "50"));

		return $fullname;
	}

	protected function addCity() {
		$notEmptyCity = new Zend_Validate_NotEmpty();
		$notEmptyCity->setMessage('Chưa chọn khu vực phụ trách!');

		$cities = new Zend_Form_Element_Select('thanh_pho_phu_trach');
		$cities->setLabel('Khu vực phụ trách')
			   ->setRequired(true)
			   ->addValidator($notEmptyCity, true)
			   ->setAttrib('key', 'value')
			   ->addFilter('StripTags')
			   ->addFilter('StringTrim')
			   ->setRegisterInArrayValidator(false);

		$cities->addMultiOption("", "  Chọn khu vực");
		$cities->addMultiOption(0, "   Cả nước");

		return $cities;
	}

	protected function addBirthday() {
		$dateValidator = new Zend_Validate_Date(array('format' => 'dd-MM-yyyy', 'locale' => 'vi'));
		
		$birthday = new Zend_Form_Element_Text('ngay_sinh');
		$birthday->setLabel(self::BIRTHDAY_LABEL)
				 ->setDescription('Định dạng dd-mm-yyyy')
				 ->addFilter('StringTrim')
				 ->setRequired(true)
				 ->addValidator('NotEmpty', true)
				 ->addValidator($dateValidator);

		return $birthday;
	}

	protected function addTel() {
		$telephone = new Zend_Form_Element_Text('dien_thoai');
		$telephone->setLabel(self::TEL_LABEL)
				  ->addFilter('StripTags')
				  ->addFilter('StringTrim')
				  ->addValidator('Digits')
				  ->setAttribs(array("maxlength" => "50", 'id' => 'tel'));

		return $telephone;
	}

	public function init() {
		$this->setName('user');
		$id = new Zend_Form_Element_Hidden('id');
		$id->addFilter('Int');

		$fullname	= self::addFullName();
		$email		= self::addEmail();
		$password	= self::addPassword();
		$role		= self::addRole();
		$city		= self::addCity();
		$birthday	= self::addBirthday();
		$tel		= self::addTel();

		$submit = new Zend_Form_Element_Submit('submit');
		$submit->setAttrib('id', 'submitbutton')
			   ->removeDecorator('DtDdWrapper')
			   ->setLabel('Save')
			   ->setAttrib("class", "btn btn-large btn-primary ");

		$reset = new Zend_Form_Element_Reset('reset');
		$reset->setAttrib('id', 'resetbutton')
			  ->removeDecorator('DtDdWrapper')
			  ->setLabel('Reset')
			  ->setAttrib("class", "btn btn-large");

		$this->addElements(array($id, $email, $password, $role, $city, $fullname, $tel, $birthday, $submit, $reset));
		$this->addDisplayGroup(array('submit', 'reset'), 'buttons');

		$dg = $this->getDisplayGroup('buttons');
		$dg->removeDecorator('Fieldset');
		$dg->removeDecorator('HtmlTag');
	}

}